package com.example.administrator.funchat.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.funchat.Contans.Config;
import com.example.administrator.funchat.R;

import org.xutils.common.Callback;
import org.xutils.http.HttpMethod;
import org.xutils.http.RequestParams;
import org.xutils.x;

import cn.jpush.android.api.JPushInterface;

public class AgreeAddFriendActivity extends AppCompatActivity {

    private TextView tv_add_friend;
    private Button btn_agree_add;
    private String content;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_agree_add_friend);
        tv_add_friend = (TextView) findViewById(R.id.tv_add_friend);
        btn_agree_add = (Button) findViewById(R.id.btn_agree_add);

        Intent intent = getIntent();
        if (null != intent) {
            Bundle bundle = getIntent().getExtras();
            content = bundle.getString(JPushInterface.EXTRA_ALERT);
            tv_add_friend.setText(content + " 请求添加好友");
        }
        btn_agree_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                RequestParams params = new RequestParams(Config.AGREE_ADD_FRIEND);
                params.addBodyParameter("mine_phone", MainActivity.u.getPhone());
                params.addBodyParameter("friend_phone", content);
                params.addBodyParameter("isAgree", "yes");
                x.http().request(HttpMethod.POST, params, new Callback.CommonCallback<String>() {
                    @Override
                    public void onSuccess(String s) {
                        if (s.equals("ok")) {
                            Toast.makeText(AgreeAddFriendActivity.this, "恭喜你，你们已经是好友了", Toast.LENGTH_SHORT).show();
                        } else if (s.equals("error")) {
                            Toast.makeText(AgreeAddFriendActivity.this, "很抱歉，服务器异常", Toast.LENGTH_SHORT).show();

                        } else {
                            Toast.makeText(AgreeAddFriendActivity.this, "请检查网络连接", Toast.LENGTH_SHORT).show();
                        }
                    }

                    @Override
                    public void onError(Throwable throwable, boolean b) {

                    }

                    @Override
                    public void onCancelled(CancelledException e) {

                    }

                    @Override
                    public void onFinished() {

                    }
                });
                finish();
            }
        });

    }


}
